{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import import_ipynb\n",
    "import os,time,sys"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "%ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from transpositionTest import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def validate_file_exists(file_path: str) -> None:\n",
    "    \"\"\"验证文件是否存在，如果不存在则退出程序\"\"\"\n",
    "    if not os.path.exists(file_path):\n",
    "        print(f\"错误: 文件 '{file_path}' 不存在。\")\n",
    "        sys.exit(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def confirm_file_overwrite(file_path: str) -> bool:\n",
    "    \"\"\"确认是否覆盖已存在的文件\"\"\"\n",
    "    if os.path.exists(file_path):\n",
    "        print(f\"警告: 文件 '{file_path}' 已存在。\")\n",
    "        response = input(\"覆盖文件? (y/n): \").lower()\n",
    "        return response == 'y'\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "def read_file_content(file_path: str) -> str:\n",
    "    \"\"\"读取文件内容，返回字符串\"\"\"\n",
    "    try:\n",
    "        with open(file_path, 'r', encoding='utf-8') as file:\n",
    "            return file.read()\n",
    "    except Exception as e:\n",
    "        print(f\"读取文件错误: {e}\")\n",
    "        sys.exit(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def write_file_content(file_path: str, content: str) -> None:\n",
    "    \"\"\"将内容写入文件\"\"\"\n",
    "    try:\n",
    "        with open(file_path, 'w', encoding='utf-8') as file:\n",
    "            file.write(content)\n",
    "        print(f\"成功写入文件: {file_path}\")\n",
    "    except Exception as e:\n",
    "        print(f\"写入文件错误: {e}\")\n",
    "        sys.exit(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_content(content: str, key: int, mode: str) -> str:\n",
    "    \"\"\"根据模式加密或解密内容\"\"\"\n",
    "    print(f\"开始处理: 模式={mode}, 密钥={key}\")\n",
    "    start_time = time.time()\n",
    "    \n",
    "    if mode == 'encrypt':\n",
    "        processed_content = encrypt_message(key, content)\n",
    "    elif mode == 'decrypt':\n",
    "        processed_content = decrypt_message(key, content)\n",
    "    else:\n",
    "        print(f\"错误: 无效模式 '{mode}'。请选择 'encrypt' 或 'decrypt'\")\n",
    "        sys.exit(1)\n",
    "    \n",
    "    elapsed_time = round(time.time() - start_time, 2)\n",
    "    print(f\"处理完成! 耗时: {elapsed_time} 秒\")\n",
    "    return processed_content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def display_summary(input_file: str, output_file: str, mode: str, key: int, \n",
    "                   input_size: int, output_size: int, elapsed_time: float) -> None:\n",
    "    \"\"\"显示处理结果摘要\"\"\"\n",
    "    print(\"\\n\" + \"=\" * 50)\n",
    "    print(\"处理摘要:\")\n",
    "    print(f\"  - 输入文件: {input_file} ({input_size} 字符)\")\n",
    "    print(f\"  - 输出文件: {output_file} ({output_size} 字符)\")\n",
    "    print(f\"  - 操作模式: {mode}\")\n",
    "    print(f\"  - 使用密钥: {key}\")\n",
    "    print(f\"  - 处理时间: {elapsed_time} 秒\")\n",
    "    print(\"=\" * 50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "    input_filename = 'frankenstein.txt'\n",
    "    output_filename = 'frankenstein.encrypted.txt'\n",
    "    key = 10\n",
    "    mode = 'encrypt'  # 'encrypt' 或 'decrypt'\n",
    "    \n",
    "    # 1. 验证输入文件\n",
    "    validate_file_exists(input_filename)\n",
    "    \n",
    "    # 2. 确认输出文件覆盖\n",
    "    if not confirm_file_overwrite(output_filename):\n",
    "        print(\"操作已取消。\")\n",
    "        sys.exit(0)\n",
    "    \n",
    "    # 3. 读取文件内容\n",
    "    content = read_file_content(input_filename)\n",
    "    print(f\"已读取文件: {input_filename} ({len(content)} 字符)\")\n",
    "    \n",
    "    # 4. 处理内容（加密/解密）\n",
    "    start_time = time.time()\n",
    "    processed_content = process_content(content, key, mode)\n",
    "    elapsed_time = round(time.time() - start_time, 2)\n",
    "    \n",
    "    # 5. 写入输出文件\n",
    "    write_file_content(output_filename, processed_content)\n",
    "    \n",
    "    # 6. 显示摘要信息\n",
    "    display_summary(\n",
    "        input_filename, output_filename, mode, key,\n",
    "        len(content), len(processed_content), elapsed_time\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
